using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qLIEFDAT = from m in mtemp.LIEFDAT select m;
       DBMapper.MapLIEFDAT(qLIEFDAT.ToList(), true);
       */
        public List<LIEFDAT> MapLIEFDAT(XElement item2, bool mergedb)
        {
            List<LIEFDAT> result = new List<LIEFDAT>();
            var item4 = from item3 in item2.Elements()
                        select new LIEFDAT
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            LSMANDANT = (string)item3.Element("LSMANDANT"),
                            LSLIEFNR = (string)item3.Element("LSLIEFNR"),
                            LSLIEFNA = (string)item3.Element("LSLIEFNA"),
                            LSNAME1 = (string)item3.Element("LSNAME1"),
                            LSNAME2 = (string)item3.Element("LSNAME2"),
                            LSSTRASSE1 = (string)item3.Element("LSSTRASSE1"),
                            LSLAND = (string)item3.Element("LSLAND"),
                            LSPLZ = (string)item3.Element("LSPLZ"),
                            LSORT = (string)item3.Element("LSORT"),
                            LSTELEX = (string)item3.Element("LSTELEX"),
                            LSMAILADR = (string)item3.Element("LSMAILADR"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapLIEFDAT(result, mergedb);
          return result;
        }
        ///DBMapper.MapLIEFDAT(mtemp.LIEFDAT.ToList<LIEFDAT>(), true);        
       public void MapLIEFDAT(List<LIEFDAT> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from LIEFDAT row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.LIEFDAT.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.LIEFDAT.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.LIEFDAT.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "LIEFDAT")]
    public class LIEFDAT : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _LSMANDANT="0";
        [Column]
        public string LSMANDANT
        {
            get
            {
                return _LSMANDANT;
            }
            set
            {
                if (value != _LSMANDANT)
                {
                     _LSMANDANT = value;
                    NotifyPropertyChanged("LSMANDANT");
                }
            }
        }
        private string  _LSLIEFNR="0";
        [Column]
        public string LSLIEFNR
        {
            get
            {
                return _LSLIEFNR;
            }
            set
            {
                if (value != _LSLIEFNR)
                {
                     _LSLIEFNR = value;
                    NotifyPropertyChanged("LSLIEFNR");
                }
            }
        }
        private string  _LSLIEFNA="0";
        [Column]
        public string LSLIEFNA
        {
            get
            {
                return _LSLIEFNA;
            }
            set
            {
                if (value != _LSLIEFNA)
                {
                     _LSLIEFNA = value;
                    NotifyPropertyChanged("LSLIEFNA");
                }
            }
        }
        private string  _LSNAME1="0";
        [Column]
        public string LSNAME1
        {
            get
            {
                return _LSNAME1;
            }
            set
            {
                if (value != _LSNAME1)
                {
                     _LSNAME1 = value;
                    NotifyPropertyChanged("LSNAME1");
                }
            }
        }
        private string  _LSNAME2="0";
        [Column]
        public string LSNAME2
        {
            get
            {
                return _LSNAME2;
            }
            set
            {
                if (value != _LSNAME2)
                {
                     _LSNAME2 = value;
                    NotifyPropertyChanged("LSNAME2");
                }
            }
        }
        private string  _LSSTRASSE1="0";
        [Column]
        public string LSSTRASSE1
        {
            get
            {
                return _LSSTRASSE1;
            }
            set
            {
                if (value != _LSSTRASSE1)
                {
                     _LSSTRASSE1 = value;
                    NotifyPropertyChanged("LSSTRASSE1");
                }
            }
        }
        private string  _LSLAND="0";
        [Column]
        public string LSLAND
        {
            get
            {
                return _LSLAND;
            }
            set
            {
                if (value != _LSLAND)
                {
                     _LSLAND = value;
                    NotifyPropertyChanged("LSLAND");
                }
            }
        }
        private string  _LSPLZ="0";
        [Column]
        public string LSPLZ
        {
            get
            {
                return _LSPLZ;
            }
            set
            {
                if (value != _LSPLZ)
                {
                     _LSPLZ = value;
                    NotifyPropertyChanged("LSPLZ");
                }
            }
        }
        private string  _LSORT="0";
        [Column]
        public string LSORT
        {
            get
            {
                return _LSORT;
            }
            set
            {
                if (value != _LSORT)
                {
                     _LSORT = value;
                    NotifyPropertyChanged("LSORT");
                }
            }
        }
        private string  _LSTELEX="0";
        [Column]
        public string LSTELEX
        {
            get
            {
                return _LSTELEX;
            }
            set
            {
                if (value != _LSTELEX)
                {
                     _LSTELEX = value;
                    NotifyPropertyChanged("LSTELEX");
                }
            }
        }
        private string  _LSMAILADR="0";
        [Column]
        public string LSMAILADR
        {
            get
            {
                return _LSMAILADR;
            }
            set
            {
                if (value != _LSMAILADR)
                {
                     _LSMAILADR = value;
                    NotifyPropertyChanged("LSMAILADR");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<LIEFDAT> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\LIEFDAT\\LIEFDAT" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\LIEFDAT" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("LIEFDAT");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (LIEFDAT row in ListInput)
                {
                    XElement f = new XElement("LIEFDAT");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement LSMANDANT = new XElement("LSMANDANT");
                         LSMANDANT.Value = row.LSMANDANT;
                          f.Add(LSMANDANT);
                        XElement LSLIEFNR = new XElement("LSLIEFNR");
                         LSLIEFNR.Value = row.LSLIEFNR;
                          f.Add(LSLIEFNR);
                        XElement LSLIEFNA = new XElement("LSLIEFNA");
                         LSLIEFNA.Value = row.LSLIEFNA;
                          f.Add(LSLIEFNA);
                        XElement LSNAME1 = new XElement("LSNAME1");
                         LSNAME1.Value = row.LSNAME1;
                          f.Add(LSNAME1);
                        XElement LSNAME2 = new XElement("LSNAME2");
                         LSNAME2.Value = row.LSNAME2;
                          f.Add(LSNAME2);
                        XElement LSSTRASSE1 = new XElement("LSSTRASSE1");
                         LSSTRASSE1.Value = row.LSSTRASSE1;
                          f.Add(LSSTRASSE1);
                        XElement LSLAND = new XElement("LSLAND");
                         LSLAND.Value = row.LSLAND;
                          f.Add(LSLAND);
                        XElement LSPLZ = new XElement("LSPLZ");
                         LSPLZ.Value = row.LSPLZ;
                          f.Add(LSPLZ);
                        XElement LSORT = new XElement("LSORT");
                         LSORT.Value = row.LSORT;
                          f.Add(LSORT);
                        XElement LSTELEX = new XElement("LSTELEX");
                         LSTELEX.Value = row.LSTELEX;
                          f.Add(LSTELEX);
                        XElement LSMAILADR = new XElement("LSMAILADR");
                         LSMAILADR.Value = row.LSMAILADR;
                          f.Add(LSMAILADR);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
